﻿namespace JavaScript8000.Common
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// Represent dictionary of ueser tokens.
    /// </summary>
    public class UserTokens
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="UserTokens"/> class.
        /// </summary>
        public UserTokens()
        {
            this.dictionary = new List<string>();
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets dictionary of user tokens.
        /// </summary>
        private List<string> dictionary;

        #endregion

        #region Public Methods

        /// <summary>
        /// Gets user token by name.
        /// </summary>
        /// <param name="userCode">
        /// The user code of token.
        /// </param>
        /// <returns>
        /// UserToken if it is exist.
        /// </returns>
        public string GetByUserCode(int userCode)
        {
            userCode--;
            if (userCode < 0 || userCode >= this.dictionary.Count)
            {
                return null;
            }

            return this.dictionary[userCode];
        }

        /// <summary>
        /// Gets the user code. If token isn't exist in dictionary - create it.
        /// </summary>
        /// <param name="name">
        /// The name of user token.
        /// </param>
        /// <returns>
        /// User code for specified token.
        /// </returns>
        public int GetUserCode(string name)
        {
            for (int i = 0; i < this.dictionary.Count; i++)
            {
                if (this.dictionary[i] == name)
                {
                    return i + 1;
                }
            }

            this.dictionary.Add(name);
            return this.dictionary.Count;
        }

        #endregion
    }
}